/*
 * File:   parser.cpp
 * Author: ScaV
 *
 * Created on November 19, 2011, 10:02 PM
 */

#include "parser.h"
#include <iostream>
#include <cstring>

parser::parser(std::vector<lexeme*> *in) {
	str = in;
	iter = str->begin();
}

parser::parser(const parser& orig) {
}

parser::~parser() {
}

void parser::operator()() {
	std::cout << "FUNCTION" << FUNCTION() << std::endl;
}

//<FUNCTION>			->	<TYPE>[ID](){<DECLARATIONS><BODY>return[ID];}
/*
<TYPE>					[int][double]					[ID]
<DECLARATIONS>			[int][double]					[ID]
<BODY>					[ID]							[return]
*/
int parser::FUNCTION() {
	std::cout << "<" << (*iter)->getName() << ">" << std::endl;
	if ((*iter)->type == LEX_KEYWORD && (!strcmp((*iter)->getName(), "int") || !strcmp((*iter)->getName(), "double")) && TYPE()) {
		++iter;
		std::cout << "<" << (*iter)->getName() << ">" << std::endl;
		if ((*iter)->type == LEX_IDENTIFICATOR) {
			++iter;
			std::cout << "<" << (*iter)->getName() << ">" << std::endl;
			if ((*iter)->getName()[0] == '(') {
				++iter;
				std::cout << "<" << (*iter)->getName() << ">" << std::endl;
				if ((*iter)->getName()[0] == ')') {
					++iter;
					std::cout << "<" << (*iter)->getName() << ">" << std::endl;
					if ((*iter)->getName()[0] == '{') {
						++iter;
						std::cout << "<" << (*iter)->getName() << ">" << std::endl;
						if (((*iter)->type == LEX_KEYWORD) && (!strcmp((*iter)->getName(), "int") || !strcmp((*iter)->getName(), "double")) && DECLARATIONS()) {
							++iter;
							std::cout << "<" << (*iter)->getName() << ">" << std::endl;
							if ((*iter)->type == LEX_IDENTIFICATOR && BODY()) {
								++iter;
								std::cout << "<" << (*iter)->getName() << (*iter)->getType() << ">" << std::endl;
								if ((*iter)->type == LEX_KEYWORD && !strcmp((*iter)->getName(), "return")) {
									++iter;
									std::cout << "<" << (*iter)->getName() << ">" << std::endl;
									if ((*iter)->type == LEX_IDENTIFICATOR) {
										++iter;
										std::cout << "<" << (*iter)->getName() << ">" << std::endl;
										if ((*iter)->getName()[0] == ';') {
											++iter;
											std::cout << "<" << (*iter)->getName() << ">" << std::endl;
											if ((*iter)->getName()[0] == '}') {
												++iter;
												std::cout << "<" << (*iter)->getName() << ">" << std::endl;
												if ((*iter)->type == LEX_END) {
													++iter;
													std::cout << "<" << (*iter)->getName() << ">" << std::endl;
													return 1;
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	return 0;
}

int parser::DECLARATIONS() {
	return 1;
}

int parser::CYCLE_DECLARATION() {
	return 1;
}

int parser::DECLARATION() {
	return 1;
}


int parser::VARIABLES() {
	return 1;
}

int parser::CYCLE_VARIABLES() {
	return 1;
}

int parser::TYPE() {
	return 1;
}

int parser::BODY() {
	return 1;
}

int parser::CYCLE_OPERATOR() {
	return 1;
}

int parser::OPERATOR() {
	return 1;
}

int parser::EXPRESSION() {
	return 1;
}

int parser::SIMPLE() {
	return 1;
}

int parser::CYCLE_SIMPLE() {
	return 1;
}
